const API = require('../../../../utils/api')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      companyId:'',
      wxuserId:'',
      wxuserName:'',
      headimgUrl:'',
      staffList:[],
      companyName:'',
      pageNum:1,
      reachBottomShow:true,
      formShow:true,
      index:0,
      array:['请选择您的职位','管理','企业主','员工'],
      submitStatus:true,
      employeeId:'',
      form: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const userInfo = app.globalData.userInfo
      this.setData({
        companyId: options.id,
        wxuserId: userInfo.id,
        wxuserName: userInfo.nickName,
        headimgUrl: userInfo.headimgUrl,
        companyName: options.companyName
      })
      this.get_custom_list()
    },

    /**
     * 接口 - 获取员工列表
     */
    get_custom_list:function(){
      let that = this
      console.log(that.data.companyId)
      API.get_employee_list({
        companyId: that.data.companyId
      }).then( res => {
        console.log(res)
        if( res.code == 200 ){
          that.setData({
            staffList: [...that.data.staffList,...res.rows],
          })
          if( res.total <= that.data.pageNum * 10 ){
            that.setData({
                reachBottomShow: false
            })
        }
        }else{
          wx.showToast({
            title: '加载失败',
            icon:'none'
          })
        }
      })
    },

    /**
    * 接口 - 获取员工详情
    */
    get_employee_detail:function(id){
      let that = this
      API.get_employee_detail({
        id:id
      }).then( res => {
        console.log(res)
        if( res.code == 200 ){
          let form = res.data
          that.setData({
            form: form
          })
        }else{
          wx.showToast({
            title: '加载失败',
            icon:'none'
          })
        }
      })
    },

    /**
     * 路由 - 分享
     */
    routeShare:function(){
      wx.switchTab({
        url: '/pages/share/share',
      })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      let that = this
      return {
        title: that.data.wxuserName + '邀请你加入' + that.data.companyName,
        // imageUrl:'',
        path: '/pages/mine/company-manage/invite/invite?inviterUserId=' + that.data.wxuserId + '&nickName=' 
        + that.data.wxuserName + '&headimgUrl=' + that.data.headimgUrl + '&companyName='+ that.data.companyName + 
        '&companyId='+that.data.companyId,
      }
    },

    //移除
    deleteInfo:function(e){
      let that = this
      let id = e.currentTarget.id
      wx.showModal({
        title: '警告',
        content: '您确定删除该员工吗',
        success (res) {
          if (res.confirm) {
            API.delete_employee_info({
              id: id
            }).then( res => {
              if( res.code == 200 ){
                wx.showToast({
                  title: '删除成功',
                })
              }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
              }
            })
          }
        }
      })
    },

    //事件 - 显示资料填写
    showForm:function(e){
      let employeeId = e.currentTarget.id
      this.setData({
        formShow:false,
        employeeId:employeeId
      })
      this.get_employee_detail(employeeId)
    },
    
    //事件 - 职位选择
    pickChange:function(e){
      this.setData({
        index:e.detail.value
      })
    },

    //提交表单更新身份
    formSubmit:function(e){
      let index = this.data.index
      this.data.form.employeeIdentity = this.data.array[index]
      this.data.form.id = this.data.employeeId
      let form = this.data.form
      if( index == 0 ){
        wx.showToast({
          title: '请选择您的职位',
          icon:'none'
        })
        return
      }
      API.update_employee_info(form).then( res => {
        console.log(res)
        if(res.code == 200 ){
          this.setData({
            // submitStatus:false,
            formShow:true
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
      })
    },
    
    //事件 - 隐藏form资料填写
    hiddenForm:function(){
      this.setData({
        formShow:true
      })
    },

     /**
     * 触底事件
     */
    onReachBottom:function(){
      if(this.data.reachBottomShow){
          let pageNum = this.data.pageNum
          pageNum++
          this.setData({
              pageNum
          })
          this.get_custom_list()
      }
    },

    /**
     * 生命周期函数--监听页面显示
    */
    onShow:function(){
  },
})